﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.DataAccess;
using RM2012Service.Business;
using System.Data;

namespace RM2012Service.Controller
{
    public class AreaCtrl
    {
        AreaDAO dao;

        public AreaCtrl()
        {
            dao = new AreaDAO();
        }

        public AreaInfo[] GetAllArea()
        {
            AreaInfo[] list = null;
            try
            {
                DataTable dt = dao.GetAllArea();
                if (dt.Rows.Count > 0)
                {
                    list = new AreaInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new AreaInfo(dt.Rows[i]["AreaId"].ToString(),
                            dt.Rows[i]["AreaName"].ToString(),
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public AreaInfo GetAreaById(string areaId)
        {
            AreaInfo info = null;
            try
            {
                DataTable dt = dao.GetAreaById(areaId);
                if (dt.Rows.Count > 0)
                {
                        info = new AreaInfo(dt.Rows[0]["AreaId"].ToString(),
                            dt.Rows[0]["AreaName"].ToString(),
                            dt.Rows[0]["Remark"].ToString());
                }
            }
            catch
            {
                info = null;
            }
            return info;
        }

        public string GetAreaIdByName(string areaName)
        {
            return dao.GetAreaIdByName(areaName).ToString();
        }
    }
}
